clear all

cd "______________"
log using "log_Prep_and_Merge.log", replace
use "bio_yearbook_merged_revised0213.dta", clear

replace id=91 if id==.
gen han=(ethnic=="汉")
// add in some missing informationa
replace male=1 if male==.
replace birthday=194900 if name=="李敦伯"
replace birthday=195307 if name=="刘志强"

replace edu_ft_ba=1 if edu_ft_ba>=2 & edu_ft_ba!=.
replace edu_ft_highest=3 if edu_ft_ba!=. & edu_ft_highest==.
replace edu_ft_ba=edu_ft_highest-2 if edu_ft_ba==. & edu_ft_highest>1 & edu_ft_highest!=.

replace edu_ft_major=. if edu_ft_highest==1 & edu_ft_ba==0
replace edu_ft_key1=. if edu_ft_highest==1 & edu_ft_ba==0
replace edu_ft_key2=. if edu_ft_highest==1 & edu_ft_ba==0
replace edu_ft_ent=. if edu_ft_highest==1 & edu_ft_ba==0
replace edu_ft_ba=. if edu_ft_highest==1 & edu_ft_ba==0
replace  edu_ft_major=1 if edu_ft_major==0
replace edu_ft_ba=0 if  edu_ft_key1==0 & edu_ft_highest==2 & edu_ft_ba==1
replace edu_ft_highest=3 if  edu_ft_key1==1 & edu_ft_highest==2 & edu_ft_ba==1
replace edu_ft_highest=4 if edu_ft_gradenter!=. & edu_ft_high==3
rename city city_chinesename


merge m:m id year using "PM10CMLEresults_Stata16.dta"
gen manipdays=(Fxnp-Fxp)*totday
list city_chinesename city id year manipdays if _merge==2
list city_chinesename city id year manipdays if _merge==1

* Changzhi (2002), Tongchuan (2002) are missing because we cannot find bio data for the mayor/secretary, Lasa (2001-2003) are missing because they are not in the pollution data (city-yearbook?).

drop if id==51|id==109  // These two cities have reverse manipulation that doesn't make sense


sum manipdays

*inputting var34-var37 for Xianyang
replace var34=231.91 if city=="Xianyang" & year==2001
replace var34=250.04 if city=="Xianyang" & year==2002
replace var34=287.64 if city=="Xianyang" & year==2003
replace var34=338.56 if city=="Xianyang" & year==2004
replace var34=432.52 if city=="Xianyang" & year==2005
replace var34=483.97 if city=="Xianyang" & year==2006
replace var34=585.89 if city=="Xianyang" & year==2007
replace var34=764.56 if city=="Xianyang" & year==2008
replace var34=873.2 if city=="Xianyang" & year==2009
replace var34=1098.68 if city=="Xianyang" & year==2010

replace var35=53.62 if city=="Xianyang" & year==2001
replace var35=55.36 if city=="Xianyang" & year==2002
replace var35=60.41 if city=="Xianyang" & year==2003
replace var35=74.89 if city=="Xianyang" & year==2004
replace var35=85.64 if city=="Xianyang" & year==2005
replace var35=98.34 if city=="Xianyang" & year==2006
replace var35=121.22 if city=="Xianyang" & year==2007
replace var35=148.94 if city=="Xianyang" & year==2008
replace var35=157.44 if city=="Xianyang" & year==2009
replace var35=203.26 if city=="Xianyang" & year==2010

replace var36=96.24 if city=="Xianyang" & year==2001
replace var36=106.2 if city=="Xianyang" & year==2002
replace var36=123.2 if city=="Xianyang" & year==2003
replace var36=147.4 if city=="Xianyang" & year==2004
replace var36=192.6 if city=="Xianyang" & year==2005
replace var36=217.7 if city=="Xianyang" & year==2006
replace var36=266.8 if city=="Xianyang" & year==2007
replace var36=376.5 if city=="Xianyang" & year==2008
replace var36=434 if city=="Xianyang" & year==2009
replace var36=573.3 if city=="Xianyang" & year==2010

replace var37=82.05 if city=="Xianyang" & year==2001
replace var37=88.51 if city=="Xianyang" & year==2002
replace var37=103.98 if city=="Xianyang" & year==2003
replace var37=116.26 if city=="Xianyang" & year==2004
replace var37=154.32 if city=="Xianyang" & year==2005
replace var37=167.89 if city=="Xianyang" & year==2006
replace var37=197.91 if city=="Xianyang" & year==2007
replace var37=239 if city=="Xianyang" & year==2008
replace var37=281.78 if city=="Xianyang" & year==2009
replace var37=322.13 if city=="Xianyang" & year==2010


keep if _merge==3
drop _merge
drop if pmconc==.
gen poll0=pollday0/totday0
gen poll0_sq=poll0^2
gen poll0_cube=poll0^3
save "PM10_merge_16.dta", replace 


use "mayor_bio_yearbook_merged_revised0213.dta", clear
replace id=91 if id==.
replace mayor_birthday=196011 if mayor_name=="戴征社"
replace mayor_birthday=194801 if mayor_name=="章振国"
replace mayor_birthday=194900 if mayor_name=="何团经"
replace mayor_ethnicity="汉" if mayor_name=="李建昌"
replace mayor_ethnicity="汉" if mayor_name=="何团经"
replace mayor_ethnicity="汉" if mayor_name=="陈震宇"
replace mayor_male=1 if mayor_name=="李建昌"
replace mayor_male=1 if mayor_name=="何团经"
replace mayor_male=1 if mayor_name=="陈震宇"
gen mayor_han=(mayor_ethnicity=="汉")
replace mayor_edu_ft_ba=1 if  mayor_edu_ft_high>=3 &  mayor_edu_ft_high!=.
replace mayor_edu_ft_ba=0 if mayor_edu_ft_high==2
replace mayor_edu_ft_key1=. if mayor_posstart==200609 & mayor_edu_ft_high==1 & mayor_edu_ft_ba==0
replace mayor_edu_ft_key2=. if mayor_posstart==200609 & mayor_edu_ft_high==1 & mayor_edu_ft_ba==0
replace mayor_edu_ft_ent=. if mayor_posstart==200609 & mayor_edu_ft_high==1 & mayor_edu_ft_ba==0
replace mayor_edu_ft_ba=. if mayor_posstart==200609 & mayor_edu_ft_high==1 & mayor_edu_ft_ba==0
replace mayor_edu_ft_high = 2 if mayor_edu_ft_high==1 & mayor_edu_ft_ba==0
rename city city_chinesename



merge m:m id year using "PM10_merge_16.dta"

list city_chinesename  city id year manipdays if _merge==2
list city_chinesename  city id year manipdays if _merge==1

keep if _merge==3
drop _merge

save "PM10_locoff_merge.dta", replace 
log close
